<template>
  <view class="approval-statistics" :style="DynamicHeight">
    <view class="approval-statistics-header">
      <view class="header-left" @click="filterClick">
        <view>{{ selectedTime }}</view>
        <up-icon v-if="!filterPopups" name="arrow-down" size="14" color="#000"
          style="margin-left: 2px;font-weight: 700;"></up-icon>
        <up-icon v-else name="arrow-up" size="14" color="#000" style="margin-left: 2px;font-weight: 700;"></up-icon>
      </view>
      <view class="header-divider">|</view>
      <view class="header-date">{{ headerDate }}</view>
    </view>
    <!-- 内容 -->
    <view class="approval-statistics-content" :style="contentDynamicHeight">
      <!-- 我发起的 -->
      <view class="content-Initiatedbyme">
        <view class="card-header">
          <view class="card-title">我发起的</view>
          <view class="card-icon" @click="viewDetails('0')">
            <up-icon name="arrow-right" color="#9fa0a2"></up-icon>
          </view>
        </view>

        <view class="card-body" @click="viewDetails('0')">
          <!-- 发起单量 -->
          <view class="card-item">
            <view class="item-label">发起单量</view>
            <view class="item-value">{{ dataObj.submit?.total || 0 }}</view>
            <view class="item-compare">
              同比
              <view class="compare-down">
                {{ dataObj.submit?.total_percent || 0 }}%
                <!-- <up-icon name="arrow-downward" size="14" color="#f1aa5b" class="compare-icon"></up-icon> -->
              </view>
            </view>
          </view>

          <!-- 未完成单量 -->
          <view class="card-item">
            <view class="item-label">未完成单量</view>
            <view class="item-value">{{ dataObj.submit?.pending || 0 }}</view>
            <view class="item-compare">
              同比
              <view class="compare-up">
                {{ dataObj.submit?.pending_percent || 0 }}%
                <!-- <up-icon name="arrow-upward" size="14" color="#5693f6" class="compare-icon"></up-icon> -->
              </view>
            </view>
          </view>

          <!-- 流程平均耗时 -->
          <view class="card-item">
            <view class="item-label">流程平均耗时</view>
            <view class="item-value">{{ dataObj.submit?.avg_time || 0 }}</view>
            <view class="item-compare">
              同比
              <view class="compare-down">
                {{ dataObj.submit?.avg_time_percent || 0 }}%
                <!-- <up-icon name="arrow-downward" size="14" color="#f1aa5b" class="compare-icon"></up-icon> -->
              </view>
            </view>
          </view>
        </view>

        <view class="content-title">
          企业流程平均耗时<text class="compare-value">{{ dataObj.submit?.avg_time || 0 }}</text>小时
        </view>
      </view>

      <!-- 我审批的 -->
      <view class="content-Iapprovedit">
        <view class="card-header">
          <view class="card-title">我审批的</view>
          <view class="card-icon" @click="viewDetails('1')">
            <up-icon name="arrow-right" color="#9fa0a2"></up-icon>
          </view>
        </view>

        <view class="card-body" @click="viewDetails('1')">
          <!-- 发起单量 -->
          <view class="card-item">
            <view class="item-label">审批单量</view>
            <view class="item-value">{{ dataObj.check?.total || 0 }}</view>
            <view class="item-compare">
              同比
              <view class="compare-down">
                持平
              </view>
            </view>
          </view>

          <!-- 未完成单量 -->
          <view class="card-item">
            <view class="item-label">待处理单量</view>
            <view class="item-value">{{ dataObj.check?.pending || 0 }}</view>
            <view class="item-compare">
              同比
              <view class="compare-up">
                持平
              </view>
            </view>
          </view>

          <!-- 流程平均耗时 -->
          <view class="card-item">
            <view class="item-label">审批平均耗时</view>
            <view class="item-value">{{ dataObj?.check?.avg_time || 0 }}</view>
            <view class="item-compare">
              同比
              <view class="compare-down">
                持平
              </view>
            </view>
          </view>
        </view>

        <view class="content-title">
          企业审批平均耗时<text class="compare-value">{{ dataObj.check?.avg_time || 0 }}</text>小时
        </view>
      </view>

      <!-- 我的待办排行 -->
      <!-- <view class="content-Mytodolist">
        <view class="card-header">
          <view class="card-title">我的待办排行</view>
        </view>
        <view class="card-table-wrapper">
          <up-table :borderColor="'transparent'" :bgColor="'transparent'" :align="'center'" :padding="'20rpx 0'"
            :fontSize="'28rpx'" :color="'#333'" :thStyle="{ backgroundColor: 'transparent', fontWeight: '600', }">
            <up-tr>
              <up-th>排行</up-th>
              <up-th>昵称</up-th>
              <up-th>待处理数量</up-th>
              <up-th>趋势</up-th>
            </up-tr>
            <up-tr style="border-top: 1px solid #efeff1;">
              <up-td></up-td>
              <up-td></up-td>
              <up-td></up-td>
              <up-td></up-td>
            </up-tr>
          </up-table>

        </view>

        <view class="content-title">
          待处理单量在企业中排行
          <text class="compare-value"></text> /
          <text class="compare-value"></text>,请继续提升效率
        </view>
      </view> -->
    </view>

    <up-popup :show="filterPopups" @close="filterPopups = false" @open="open">
      <view :style="filterPopupsDynamicHeight" class="member-popup-content">
        <!-- 顶部操作栏 -->
        <view class="popup-header">
          <view class="close-btn" @click="filterPopups = false">

            <up-icon name="close" size="18" color="#26282b"></up-icon>
          </view>
          <view class="title">选择时间</view>
        </view>

        <!-- 内容 -->
        <view class="time-options">
          <view v-for="(item, index) in timeOptions" :key="index"
            :class="['itemView', { 'active': selectedTime === item }]" @click="selectTime(item)">
            {{ item }}
          </view>
        </view>
      </view>
    </up-popup>
  </view>
</template>

<script>
import apply from "@/apis/cloud/apply.js";
export default {
  name: 'approvalStatistics',
  data () {
    return {
      dataObj: {},
      height: 0,
      DynamicHeight: {},
      contentDynamicHeight: {},
      filterPopupsDynamicHeight: {},
      filterPopups: false,
      timeOptions: ['本周', '近7天', '上周', '近14天', '本月', '近30天', '上月'],
      selectedTime: '近7天',
      headerDate: '',
      startDate: '', // 开始时间
      endDate: '', // 结束时间
    }
  },
  async created () {
    this.height = (uni.getSystemInfoSync().windowHeight || uni.getSystemInfoSync().screenHeight) || 0
    // #ifdef APP-PLUS
    this.DynamicHeight = { height: `${this.height - 123}px`, overflowY: 'hidden' }
    this.contentDynamicHeight = { height: `${this.height - 163}px`, overflowY: 'auto' }
    this.filterPopupsDynamicHeight = { height: `${this.height / 2.7}px`, overflowY: 'auto' }
    // #endif

    // #ifdef H5
    this.DynamicHeight = { height: `${this.height - 96}px`, overflowY: 'hidden' }
    this.contentDynamicHeight = { height: `${this.height - 136}px`, overflowY: 'auto' }
    this.filterPopupsDynamicHeight = { height: `${this.height / 2.5}px`, overflowY: 'auto' }
    // #endif
    const cache = uni.getStorageSync('approval_selectedTime')
    if (cache) {
      this.selectedTime = cache
    }
    // 默认近7天
    await this.updateHeaderDate(this.selectedTime);

  },
  methods: {
    async getApplyStatistics () {
      uni.showLoading({
        title: '加载中...'
      });
      try {
        let res = await apply.ApplyStatistics({
          start: this.startDate,
          end: this.endDate
        })

        if (res.status == 1) {
          this.dataObj = res.data
        } else {
          uni.showToast({
            title: res.msg || "获取失败",
            icon: 'error'
          })
        }
      } catch (error) {
        uni.showToast({
          title: "获取失败",
          icon: "error",
        });
      } finally {
        uni.hideLoading()
      }
    },
    // 跳转页面
    viewDetails (value) {
      uni.navigateTo({
        url: `/pages/apply/components/approvalStatistics?index=${value}&selectedTime=${this.selectedTime}`
      })

    },
    // 筛选弹窗
    filterClick () {
      this.filterPopups = true
    },
    // 时间阶段选择
    selectTime (item) {
      uni.setStorageSync('approval_selectedTime', item)
      this.selectedTime = item;
      this.updateHeaderDate(item);
      this.filterPopups = false;
    },
    // 时间更换
    async updateHeaderDate (item) {
      const now = new Date();
      let startDate, endDate = now;
      switch (item) {
        case '近7天':
          startDate = new Date(now.getTime() - 6 * 24 * 3600 * 1000);
          break;
        case '本周':
          const day = now.getDay() || 7;
          startDate = new Date(now.getTime() - (day - 1) * 24 * 3600 * 1000);
          break;
        case '上周':
          const d = now.getDay() || 7;
          startDate = new Date(now.getTime() - (d + 6) * 24 * 3600 * 1000);
          endDate = new Date(now.getTime() - d * 24 * 3600 * 1000);
          break;
        case '近14天':
          startDate = new Date(now.getTime() - 13 * 24 * 3600 * 1000);
          break;
        case '本月':
          startDate = new Date(now.getFullYear(), now.getMonth(), 1);
          break;
        case '近30天':
          startDate = new Date(now.getTime() - 29 * 24 * 3600 * 1000);
          break;
        case '上月':
          const firstDayLastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
          const lastDayLastMonth = new Date(now.getFullYear(), now.getMonth(), 0);
          startDate = firstDayLastMonth;
          endDate = lastDayLastMonth;
          break;
      }

      const format = (date, value) => {
        return `${date.getFullYear()}${value ? '-' : '.'}${(date.getMonth() + 1).toString().padStart(2, '0')}${value ? '-' : '.'}${date.getDate().toString().padStart(2, '0')}`;
      }

      this.headerDate = `${format(startDate, false)}-${format(endDate, false)}`;
      this.startDate = format(startDate, true)
      this.endDate = format(endDate, true)
      await this.getApplyStatistics()
    }
  }
}

</script>

<style lang="scss" scoped>
.approval-statistics {
  overflow-x: hidden;
  color: #19232f;
  width: 100%;
  background-color: #f2f4f8;
  margin: 0;

  // 搜索
  .approval-statistics-header {
    font-size: 30rpx;
    width: 100%;
    padding: 0 10px;
    height: 40px;
    background-color: #ffffff;
    display: flex;
    align-items: center; // 垂直居中
    justify-content: flex-start;

    .header-left {
      margin-top: 2rpx;
      display: flex;
      align-items: center;
    }

    .header-divider {
      padding: 0 10rpx;
      color: #7b7c7e;
    }

    .header-date {
      margin-top: 2rpx;
      color: #7b7c7e;
    }
  }

  // 内容
  .approval-statistics-content {
    width: 100%;

    // 我发起的
    .content-Initiatedbyme {
      // #ifdef APP-PLUS
      height: 300rpx;
      // #endif

      // #ifdef H5
      height: 330rpx;
      // #endif
      background-color: #fff;
      margin-top: 10px;
      position: relative;
      padding: 10px 10px 0 10px;

      .card-header {
        display: flex;
        height: 50rpx;

        .card-title {
          font-size: 30rpx;
          color: #19232f;
        }

        .card-icon {
          margin-top: 5px;
          position: absolute;
          right: 10rpx;
          top: 14rpx;
        }
      }

      .card-body {
        display: flex;
        height: auto;
        margin-top: 10rpx;

        .card-item {
          flex: 1;
          background-color: #f2f4f8;
          padding: 18rpx 10rpx 18rpx 10rpx;
          border-radius: 20rpx;

          &:not(:first-child) {
            margin-left: 5px;
          }

          .item-label {
            color: #9b9da0;
            font-size: 30rpx;
          }

          .item-value {
            padding: 10rpx 0;
            font-size: 45rpx;
            font-weight: 700;
          }

          .item-compare {
            color: #b1b3b6;
            display: flex;
            font-size: 29rpx;

            .compare-up {
              color: #5693f6;
              display: flex;
              margin-left: 5rpx;
            }

            .compare-down {
              color: #f1aa5b;
              display: flex;
              margin-left: 5rpx;
            }

            .compare-icon {
              margin-left: 4rpx;
            }
          }
        }
      }

      .content-title {
        display: flex;
        color: #484a4c;
        font-size: 28rpx;
        margin-top: 6px;

        .compare-value {
          color: #f2a64f;
          margin: 0 5px;
        }
      }
    }

    // 我审批的
    .content-Iapprovedit {
      // #ifdef APP-PLUS
      height: 300rpx;
      // #endif

      // #ifdef H5
      height: 330rpx;
      // #endif
      background-color: #fff;
      margin-top: 10px;
      position: relative;
      padding: 10px 10px 0 10px;

      .card-header {
        display: flex;
        height: 50rpx;

        .card-title {
          font-size: 30rpx;
          color: #19232f;
        }

        .card-icon {
          margin-top: 5px;
          position: absolute;
          right: 10rpx;
          top: 14rpx;
        }
      }

      .card-body {
        display: flex;
        height: auto;
        margin-top: 10rpx;

        .card-item {
          flex: 1;
          background-color: #f2f4f8;
          padding: 18rpx 10rpx 18rpx 10rpx;
          border-radius: 20rpx;

          &:not(:first-child) {
            margin-left: 5px;
          }

          .item-label {
            color: #9b9da0;
            font-size: 30rpx;
          }

          .item-value {
            padding: 10rpx 0;
            font-size: 45rpx;
            font-weight: 700;
          }

          .item-compare {
            color: #b1b3b6;
            display: flex;
            font-size: 29rpx;

            .compare-up {
              color: #5693f6;
              display: flex;
              margin-left: 5rpx;
            }

            .compare-down {
              display: flex;
              margin-left: 5rpx;
            }

            .compare-icon {
              margin-left: 4rpx;
            }
          }
        }
      }

      .content-title {
        display: flex;
        color: #484a4c;
        font-size: 28rpx;
        margin-top: 6px;

        .compare-value {
          color: #f2a64f;
          margin: 0 5px;
        }
      }
    }

    // 我的待办排行
    .content-Mytodolist {
      // #ifdef APP-PLUS
      height: 290rpx;
      // #endif

      // #ifdef H5
      height: 310rpx;
      // #endif
      background-color: #fff;
      margin-top: 10px;
      position: relative;
      padding: 10px 10px 0 10px;

      .card-header {
        display: flex;
        height: 50rpx;

        .card-title {
          font-size: 30rpx;
          color: #19232f;
        }

        .card-icon {
          margin-top: 5px;
          position: absolute;
          right: 10rpx;
          top: 14rpx;
        }
      }

      .card-table-wrapper {
        margin-top: 0px;
      }


      .content-title {
        border-radius: 5px;
        padding: 5px;
        background-color: #f6f6f6;
        display: flex;
        color: #484a4c;
        font-size: 28rpx;
        margin-top: 6px;

        .compare-value {
          color: #f2a64f;
          margin: 0 5px;
        }
      }
    }


  }


  .member-popup-content {
    background-color: #fff;
    padding: 0 30rpx;
    box-sizing: border-box;

    .popup-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 30rpx 0;
      border-bottom: 1rpx solid #f5f5f5;
      position: relative;

      .title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        width: 200rpx;
        text-align: center;
      }

    }



    .time-options {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      padding: 15px;
      justify-content: space-between;

      .itemView {
        width: calc(50% - 5px);
        text-align: center;
        padding: 7px 0;
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        background-color: #fff;
        box-sizing: border-box;
      }

      .itemView.active,
      .header-left .active {
        background-color: #5693f6;
        color: #fff;
      }
    }

  }
}
</style>